using System;
using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Document
{
    public class JavaScriptFoldingStrategy : IFoldingStrategy
    {
        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            return GenerateFoldMarkers(document);
        }

        public List<FoldMarker> GenerateFoldMarkers(IDocument document)
        {
            List<FoldMarker> list = new List<FoldMarker>();
            int num = 0;
            Stack<int> stack = new Stack<int>();
            Stack<string> stack2 = new Stack<string>();
            //int num2 = 0;
            Stack<int> stack3 = new Stack<int>();
            Stack<string> stack4 = new Stack<string>();
            checked
            {
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    int textLength = document.TextLength;
                    LineSegment lineSegment = document.GetLineSegment(i);
                    int j;
                    for (j = lineSegment.Offset; j < textLength; j++)
                    {
                        char charAt;
                        if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
                        {
                            break;
                        }
                    }
                    if (j == textLength)
                    {
                        break;
                    }
                    int num3 = j - lineSegment.Offset;
                    string text = document.GetText(document.GetLineSegment(i));
                    if (text.Trim().IndexOf("</script>") == -1 && text.Trim().StartsWith("<script", StringComparison.CurrentCultureIgnoreCase))
                    {
                        stack.Push(i);
                        stack2.Push(text);
                        continue;
                    }
                    if (text.Trim().IndexOf("<script") == -1 && text.Trim().StartsWith("</script>", StringComparison.CurrentCultureIgnoreCase) && stack.Count > 0)
                    {
                        num = stack.Pop();
                        string text2 = stack2.Pop();
                        string foldText = "<script>";
                        list.Add(new FoldMarker(document, num, document.GetLineSegment(num).Length - text2.TrimStart().Length, i, num3 + text2.Length * 2 + "</script>".Length, FoldType.Unspecified, foldText));
                    }
                    if ((text.Trim().StartsWith("{") || text.Trim().EndsWith("{")) && (text.Trim().StartsWith("{") || (text.Trim().IndexOf("}") == -1 && text.Trim().EndsWith("{"))))
                    {
                        stack3.Push(i);
                    }
                    else if (text.Trim().StartsWith("}") && stack3.Count > 0)
                    {
                        int num4 = stack3.Pop();
                        list.Add(new FoldMarker(document, num4, document.GetLineSegment(num4).Length - "{".Length, i, 57, FoldType.TypeBody, "{...}"));
                    }
                }
                return list;
            }
        }
    }
}
